import React, { Component } from 'react'

export default class index extends Component {
    state = {
        count: 0
    }
    add = () => {
        const { count } = this.state

        /**
         * 1.对象式setState
         */
        // setState 方法接收第二个参数，当更新完毕后会触发回调
        this.setState({ count: count + 1 },()=>{
            console.log('更新后的的count：' + this.state.count);
        })

        /**
         * 2.函数式setState
         */
        this.setState((state,props) => {
            console.log(props);
            return {
                count: state.count + 1
            }
        }, () => {
            console.log('更新后的的count：' + this.state.count);
        })
    }
    render() {
        return (
            <div>
                <h2>当前的数据和是：{this.state.count}</h2>
                <button onClick={this.add}>点我加1</button>
            </div>
        )
    }
}
